package com.bobo.web.provider.config;

//import com.deyi.deyi.web.provider.util.RestClientHelper;

import com.bobo.web.provider.util.commutils.RestClientHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Map;

/**
 * RestTemplateHelper
 * 用于打印请求消耗时间
 *
 * @author:v
 * @version:1.0
 * @date: 2018/10/19
 */
public class RestTemplateHelper extends RestTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateHelper.class);

    public RestTemplateHelper() {
        super();
    }

    public RestTemplateHelper(HttpComponentsClientHttpRequestFactory factory) {
        super(factory);
    }

    @Override
    public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
        long startTime = System.currentTimeMillis();
        try {
            return super.getForEntity(url, responseType, uriVariables);
        } finally {
            LOGGER.info("call {} cost {}ms", RestClientHelper.getUrl(url, uriVariables), System.currentTimeMillis() - startTime);
        }
    }

    @Override
    public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
        long startTime = System.currentTimeMillis();
        try {
            return super.getForEntity(url, responseType, uriVariables);
        } finally {
            LOGGER.info("call {} cost {}ms", url, System.currentTimeMillis() - startTime);
        }
    }

    @Override
    public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {
        long startTime = System.currentTimeMillis();
        try {
            return super.getForEntity(url, responseType);
        } finally {
            LOGGER.info("call {} cost {}ms", url, System.currentTimeMillis() - startTime);
        }
    }


    @Override
    public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException {
        long startTime = System.currentTimeMillis();
        try {
            return super.postForEntity(url, request, responseType, uriVariables);
        } finally {
            LOGGER.info("call {} cost {}ms", RestClientHelper.getUrl(url, uriVariables), System.currentTimeMillis() - startTime);
        }
    }

    @Override
    public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
        long startTime = System.currentTimeMillis();
        try {
            return super.postForEntity(url, request, responseType, uriVariables);
        } finally {
            LOGGER.info("call {} cost {}ms", url, System.currentTimeMillis() - startTime);
        }
    }

    @Override
    public <T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws RestClientException {
        long startTime = System.currentTimeMillis();
        try {
            return super.postForEntity(url, request, responseType);
        } finally {
            LOGGER.info("call {} cost {}ms", url, System.currentTimeMillis() - startTime);
        }
    }
}
